//模块主窗口
BlacklistManagerPanel = Ext.extend(Ext.Panel,{
	 closable: true,
   	 autoScroll:true,
   	 layout:'border',
   	 width:'100%',
   	 initComponent : function() {
			var mp = this ;
   			this.tbar =  this.buildBbar(mp) ;
   			var gd = new BlacklistManagerGridPanel({region : "center"}) ;
   			this.items = [gd] ;
   		BlacklistManagerPanel.superclass.initComponent.call(this);
   	},
   	deleteUser: function(btn) {
   		var gp = btn.ownerCt.ownerCt.items.get(0) ;
   		var rows= gp.getSelectionModel().getSelections(); 
   		if(rows.length==0) {
   			 MsgTip.msg({
			 	msg:"请至少选择一项!",
			 	cmp: gp,
			 	pos:"center"
			 });
   			 return ;
   		}
   		var ids = "";
   		for(var i=0 ;i<rows.length;i++) {
   			if(i==0) {
   				ids = rows[i].data['id'] ;
   			}else{
   				ids = ids +","+rows[i].data['id'] ;
   			}
   		}
   		Ext.MessageBox.confirm("确认", "确认要删除这些用户吗?", function(btn) {
					if (btn == "yes") {
						Ext.Ajax.request( {
							waitMsg : '正在执行  ',
							method : 'POST',
							params : {
								ids:ids 
							},
							url : 'main_deleteStaffById.action',
							scope : this,
							success : function(request) {
								var Result=Ext.decode(request.responseText);
								if(Result.success) {
									gp.getStore().reload();
								} else {
									 Ext.Msg.alert("错误",Result.info );
								}
							},
							failure : function() {
								Ext.Msg.alert("错误", "与后台联系的时候出现了问题");
							}
						});
						
					}
				});
   		
   	},
   	buildBbar: function(mp) {
   		return [{
   				text: "删除用户",
   				handler:this.deleteUser,
    			iconCls: 'ico-task-delete'
   			},'->',{
   				 xtype:'textfield',
   				 ref: '../searchText',
   				 width:200,
	             emptyText:'请输入QQ号码查询',
	             listeners:{
	                 'specialkey':function(f,e) {
	                 	 if(e.getKey() == e.ENTER){
	                 	 		var doSearch = mp.doSearch ;
	                 	 		mp.queryUserList(doSearch);	                 		 
	                 	 }
	                 }
   				}
   			},{xtype:'spacer',width:5},{
   				text : "查询",
   				ref:"../doSearch",
   				handler :this.queryUserList,
   				iconCls: 'ico-dot-search'
   			 }] ;
   	},queryUserList:function(btn){
   		var tp =  btn.ownerCt.ownerCt ;
   		var gp = tp.items.get(0);
   		gp.recordStart = 1;
   		var text = tp.searchText.getValue() ;
   		gp.store.baseParams = {
   			'qq':text
   		} ;
   		gp.store.load({params:{start:0, limit:25}});
   	}
});

//高额用户信息列表
BlacklistManagerGridPanel = Ext.extend(Ext.grid.EditorGridPanel,{
	header: false,
	loadMask:{msg:'加载中...'},
	enableDragDrop: false,
	viewConfig :{
        forceFit: true,
        headerAlign:'center'
    },
	initComponent : function() {
       	var thisGrid = this;
       	this.recordStart = 1 ;
    	var userCm = new Ext.grid.CheckboxSelectionModel({
				dataIndex: "uid",
                sortable: false,
                singleSelect:false,
                width:20
		});
		var rn = new　Ext.grid.RowNumberer({
		　　header　:　"序号",
		　　width　:　35,
		　　renderer:function(value,metadata,record,rowIndex){
		　　　return　　thisGrid.recordStart　+　rowIndex;
		　　}
		});
		this.columns　=　[userCm,rn,　
		    {
				header:'名称',
				 align:'right',
				dataIndex:"nick"
			},{
		        header:'QQ号码',
		         align:'right',
				dataIndex:"qq",
				width:100
			},{
		        header:'封禁天数',
				sortable:true,
				dataIndex:"day",
				align:'right',
				width:80
			},{
		        header:'封禁范围',
		        align:'center',
				dataIndex:"cluster",
				width:100,
				renderer:function(v) {
					if(!v ||v==0 ||v=='0') {
						return "全局封禁";
					} else {
						return "群内封禁";
					}
				}
			},{
		        header:'封禁原因',
				sortable:true,
				 align:'center',
				dataIndex:"keword"
				,width:80
			},{
		        header:'解封日期',
				sortable:true,
				 align:'center',
				dataIndex:"endTime"
			},{
		        header:'最后更<br/>新时间',
				sortable:true,
				 align:'center',
				dataIndex:"updateTime"
			},{
				hidden:true,
				dataIndex:"id",
				width:70
			}
		] ;
		var dataStore = new Ext.data.JsonStore({
			url : 'action.php?action=studyManager&method=getBlacklist',
			root: 'root',
	        totalProperty: 'totalProperty',
	        fields: [
	           'id','nick','qq','day','keword','updateTime','endTime','cluster'
	        ]
		});
	
		this.store = dataStore;
		this.sm = userCm;
		this.bbar = new Ext.PagingToolbar({
			pageSize : 25,
			store : dataStore,
			displayInfo : true,
			displayMsg : '第 {0} - {1} 条  共 {2} 条',
			emptyMsg : "没有记录",
			listeners:{
				beforechange:function(pt,obj) {
					thisGrid.recordStart = obj.start+1 ;
				}
			}
		});
//		dataStore.load({
//			params:{
//				start:0,
//				limit:25
//			}
//		});
	BlacklistManagerGridPanel.superclass.initComponent.call(this);
	}
    ,listeners:{
    	afteredit:function(e) {
    		//alert(e.value);
    		var res = e.record ;
    		var field = e.field ;
    		if(field=="isService") {
    			field = "is_service";
    		} else if(field=="isBlacklist") {
    			field = 'is_blacklist';
    		} else if(field=="studyTime") {
    			field = 'study_time';
    		} else if(field=='roleName') {
    			e.value = e.grid.upValue;
    			field = 'roleIds';
    		}
    		var id = res.data['id'];
    		if(e.value && e.value!=null && e.value!=e.originalValue) {
    			e.grid.updateField(id,field,e.value,e.grid);
    		}
    	}
    }
    ,updateField:function(id,fieldName,value,grid) {
		//alert(id+filedName+value);
		Ext.Ajax.request({
			url:'action.php?action=cluster&method=updateFiled',
			method:'POST',
			params:{
				'id':id,
				'fieldName':fieldName,
				'value':value
			},
			success:function(response) {
				var Result=Ext.decode(response.responseText);
				if(Result.success) {
					MsgTip.msg({
					 	msg:"修改成功~",
					 	cmp: grid,
					 	pos:"center"
					 });
				} else {
					MsgTip.msg({
					 	msg:"修改失败~",
					 	cmp: grid,
					 	pos:"center"
					 });
				}
			}
		});
	}
});
